<?php
/**
 * 乐天账号
 * User: bgl
 * Date: 2022/12/13
 * Time: 11:06
 */
namespace App\Admin\Controllers\Basics;

use App\Admin\Ajax\Basics\RakutenAccountAjax;
use App\Models\Basics\RakutenAccount;
use App\Models\Basics\SysPlat;
use App\Models\Basics\SysPlatAccount;
use App\Models\Product\ProductRakutenSku;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class RakutenAccountController extends AdminController{

    protected $title = 'Rakuten店铺账号';

    protected function grid()
    {
        $grid = new Grid(new RakutenAccount());
        $grid->model()->orderBy('id','DESC');
        $grid->column('id','ID')->style('width: 5%')->sortable();
        $grid->column('store','店铺')->help('可模糊查询');
        $grid->column('account','账号')->help('可模糊查询');
        $grid->column('auth_status','授权状态')->using(RakutenAccount::$_authStatus)->filter(RakutenAccount::$_authStatus)->label([1 => 'success', 0 => 'default']);
        $grid->column('expire_time','到期时间')->display(function ($value){
            if($value){
                $expireTime = strtotime($value); //过期时间
                $currentTime = time(); //当前时间
                $waringTime = strtotime("+7 day"); //预警时间(提醒)
                if($expireTime >= $currentTime){
                    if($expireTime <= $waringTime){
                        $str = "<span class='label-warning'>".$value." (授权, 即将到期)</span>";
                    }else{
                        $str = "<span class='label-success' >".$value." (授权中)</span>";
                    }
                }else{
                    $str = "<span class='label-danger'>".$value." (授权到期)</span>";
                }
                return $str;
            }else{
                return '异常';
            }
        })->help('rakutenAPI授权时间为三个月, 请及时更新授权秘钥.');
        $grid->column('status','状态')->using(RakutenAccount::$_status)->filter(RakutenAccount::$_status)->label([1 => 'info', 0 => 'danger']);

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->paginate(10);
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->quickSearch('store','account');
        $grid->disableFilter();

        return $grid;
    }

    public function ajax(){
        $action = request('action');
        $param = request('param');
        $ajax = new RakutenAccountAjax();
        return $param ? call_user_func([$ajax,$action],$param) : call_user_func([$ajax,$action]);
    }

    public function form()
    {
        $form = new Form(new RakutenAccount());

        $form->hidden('id');
        $form->text('store','店铺');
        $form->text('account', '账号')
            ->creationRules(['required', "unique:b_rakuten_account"],['required' => '账号必填','unique' => '账号已存在'])
            ->updateRules(['required', "unique:b_rakuten_account,account,{{id}}"],['required' => '账号必填','unique' => '账号已存在']);
        $form->text('authorization', '秘钥');
        $form->text('site', '授权网址');
        $form->date('expire_time', '到期时间');
        $form->radio('status','状态')->options(RakutenAccount::$_status)->default(1);

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        //回写平台账号
        $form->saving(function (Form $form) {

            if($form->isCreating()){
                //创建,验证平台账号
                $platAccount = SysPlatAccount::query()->where('account', $form->account)->exists();
                if ($platAccount) {
                    admin_error('平台账号重复', '该账号已存在-1');
                    return back()->withInput();
                }
            }

            if($form->isEditing()){
                //更新,验证平台账号
                $platAccount = SysPlatAccount::query()->where('id', '<>', $form->model()->id)->where('account', $form->account)->exists();
                if ($platAccount) {
                    admin_error('平台账号重复', '该账号已存在-2');
                    return back()->withInput();
                }

                if($form->model()->account != $form->account){
                    SysPlatAccount::updatePlatAccount($form->model()->id, SysPlat::PLAT_RAKUTEN, $form->account); //更新平台账号
                }
            }
        });

        //回写平台账号
        $form->saved(function (Form $form) {

            if($form->isCreating()){
                SysPlatAccount::createPlatAccount($form->model()->id, SysPlat::PLAT_RAKUTEN, $form->model()->account); //创建平台账号
            }

            if($form->model()->status == 0){
                ProductRakutenSku::query()
                    ->where('account_id',$form->model()->id)
                    ->where('status',1)
                    ->update(['status' => 2]);
            }
        });

        return $form;
    }

}
